package pres.wchen.bussiness.cor.shiro.service;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pres.wchen.bussiness.dao.SysPermissionMapper;
import pres.wchen.bussiness.vo.SysPermissionVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者： wchen
 * @描述：
 * @创建时间： 2018/1/22 11:30
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
@Service
public class ShiroConfigService {
    private static final Logger logger = LoggerFactory.getLogger(ShiroConfigService.class);

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private ShiroFilterFactoryBean shiroFilterFactoryBean;

    /**
     *
     * @描述： 初始化权限资源配置
     * @名称： initShiroPermission
     * @入参： [filterChainDefinitionMap]
     * @出参： void
     * @修改信息： 创建：wchen 2018/1/22
     */
    public Map initShiroPermission(){
        Map filterChainDefinitionMap = new HashMap();
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/login/", "anon");
        filterChainDefinitionMap.put("/logout", "logout");
        Map conditionMap = new HashMap();
        conditionMap.put("pageFlag", false);
        List<SysPermissionVO> permissionList = sysPermissionMapper.list(conditionMap);
        for(SysPermissionVO sysPermission : permissionList) {
            filterChainDefinitionMap.put(sysPermission.getPermPath(), "perms[" + sysPermission.getPermRegulation() + "]");
        }
        filterChainDefinitionMap.put("/**", "authc");
        logger.info("**********************资源变更完毕********************");
        return conditionMap;
    }


    /**
     * 重新加载权限
     */
    public void updatePermission() {
        synchronized (shiroFilterFactoryBean) {
            AbstractShiroFilter shiroFilter = null;
            try {
                shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject();
            } catch (Exception e) {
                throw new RuntimeException("get ShiroFilter from shiroFilterFactoryBean error!");
            }
            PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
            DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager();
            // 清空老的权限控制
            manager.getFilterChains().clear();

            shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
            shiroFilterFactoryBean.setFilterChainDefinitionMap(initShiroPermission());
            // 重新构建生成
            Map<String, String> chains = shiroFilterFactoryBean.getFilterChainDefinitionMap();
            for (Map.Entry<String, String> entry : chains.entrySet()) {
                String url = entry.getKey();
                String chainDefinition = entry.getValue().trim().replace(" ", "");
                manager.createChain(url, chainDefinition);
            }
            System.out.println("更新权限成功！！");
        }
    }

}
